package com.dodol.common;

import com.dodol.util.ElementFinder;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
 * 预期结果验证
 */
public class ExpectedVerification {
    // 校验类型，值对比，验证
    private Vertiy verityType;
    // 校验值
    private String verityValue;
    // 当前driver
    private WebDriver driver;
    // 定位数据
    private ElementFinder.Element element;

    public ExpectedVerification() {

    }

    public ExpectedVerification(Vertiy verityType, String verityValue) {
        this.verityType = verityType;
        this.verityValue = verityValue;
    }

    public ExpectedVerification(Vertiy verityType, String verityValue, ElementFinder.Element element) {
        this.verityType = verityType;
        this.verityValue = verityValue;
        this.element = element;
    }

    public ExpectedVerification(Vertiy verityType, String verityValue, WebDriver driver) {
        this.verityType = verityType;
        this.verityValue = verityValue;
        this.driver = driver;
    }


    public boolean verity() {
        switch (verityType) {
            case EXIST:
                return element != null;
            case DISPLAY:
                return isElementDisplay(element);
            case ENABLED:
                return isElementEnabled(element);
            case SELECTED:
                return isElementSelected(element);
            case URL_EXIST:
                return isUrlExist(driver);
            case VALUE:
                return isElementValueEqual(verityValue, element);
            case TITLE:
                return assertPageTitle(verityValue, driver);
            default:
                return false;
        }
    }


    /**
     * 校验url是否存在
     *
     * @param driver 当前driver，用来获取当前url
     * @return
     */
    private boolean isUrlExist(WebDriver driver) {
        if (driver != null) {
            return verityValue.contains(driver.getCurrentUrl());
        }
        return false;
    }

    /**
     * 某个元素是否显示
     *
     * @param el 元素描述对象
     * @return
     */
    private boolean isElementDisplay(ElementFinder.Element el) {
        WebElement element = ElementFinder.findElement(driver, el);
        return element.isDisplayed();
    }

    /**
     * 某个元素是否许可访问
     *
     * @param el
     * @return
     */
    private boolean isElementEnabled(ElementFinder.Element el) {
        WebElement element = ElementFinder.findElement(driver, el);
        return element.isEnabled();
    }

    /**
     * 元素是否被选中
     *
     * @param el
     * @return
     */
    private boolean isElementSelected(ElementFinder.Element el) {
        WebElement element = ElementFinder.findElement(driver, el);
        return element.isSelected();
    }

    /**
     * 验证预期值和元素实际值是否一致
     *
     * @param verityValue 预期值
     * @param el          目标元素标识
     * @return
     */
    private boolean isElementValueEqual(String verityValue, ElementFinder.Element el) {
        WebElement element = ElementFinder.findElement(driver, el);
        return element.getText().equals(verityValue);
    }

    /**
     * 当前页面标题是否一致
     *
     * @param expectedValue
     * @param driver
     * @return
     */
    public boolean assertPageTitle(String expectedValue, WebDriver driver) {
        return driver.getTitle().equals(expectedValue);
    }

    public Vertiy getVerityType() {
        return verityType;
    }

    public void setVerityType(Vertiy verityType) {
        this.verityType = verityType;
    }

    public String getVerityValue() {
        return verityValue;
    }

    public void setVerityValue(String verityValue) {
        this.verityValue = verityValue;
    }

    public WebDriver getDriver() {
        return driver;
    }

    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    public ElementFinder.Element getElement() {
        return element;
    }

    public void setElement(ElementFinder.Element element) {
        this.element = element;
    }

    @Override
    public String toString() {
        if (verityType != null && verityValue != null) {
            return "验证方式：" + verityType.toString() + "--验证Value：" + verityValue;
        }
        return super.toString();
    }


}


